/* eslint-disable max-len */
/* eslint-disable complexity */
import { useDragAndDropRules } from "../../../designer-canvas/src/composition/rule/use-drag-drop-rules";
import { DraggingResolveContext, UseDesignerRules } from "../../../designer-canvas/src/composition/types";
import { ComponentSchema, DesignerItemContext } from "../../../designer-canvas/src/types";

export function useDesignerRules(schema: ComponentSchema, designerItemContext?: DesignerItemContext): UseDesignerRules {

    const dragAndDropRules = useDragAndDropRules();

    let canAcceptChildContent = true;

    let hideNestedPadding = false;

    let schemaContext: any;

    let isInFixedContextRules = false;

    /**
     * 判断是否可以接收拖拽新增的子级控件
     */
    function canAccepts(draggingContext: DraggingResolveContext): boolean {
        return false;
    }

    function checkCanDeleteComponent(): boolean {
        return !isInFixedContextRules;
    }

    function checkCanMoveComponent(): boolean {
        return !isInFixedContextRules;
    }

    function hideNestedPaddingInDesginerView() {
        return hideNestedPadding;
    }

    /**
     * 判断当前容器的上下文
     */
    function resolveComponentContext() {

        const component = schema;
        // 控件本身样式
        const componentClass = component.appearance && component.appearance.class || '';
        const componentClassList = componentClass ? componentClass.split(' ') : [];

        // 父级节点
        const parentSchema = designerItemContext?.schema;
        const parentClass = parentSchema && parentSchema.appearance && parentSchema.appearance.class || '';
        const parentClassList = parentClass ? parentClass.split(' ') : [];

        // 子级节点
        const childContents = component.contents || [];
        const firstChildContent = childContents.length ? childContents[0] : null;
        const firstChildClass = (firstChildContent && firstChildContent.appearance && firstChildContent.appearance.class) || '';
        const firstChildClassList = firstChildClass ? firstChildClass.split(' ') : [];
        const childrenClassList = childContents.length ? childContents.reduce((classList: string[], childComponentSchema: ComponentSchema) => {
            const childCompoentClass = childComponentSchema && childComponentSchema.appearance ? childComponentSchema.appearance.class : '';
            const childCompoentClassList = childCompoentClass ? childCompoentClass.split(' ') : [];
            classList.push(...childCompoentClassList);
            return classList;
        }, []) : [];

        const grandParent = designerItemContext?.parent && designerItemContext.parent.schema;
        const grandParentClass = grandParent && grandParent.appearance && grandParent.appearance.class || '';
        const grandParentClassList = grandParentClass ? grandParentClass.split(' ') : [];

        schemaContext = {
            componentClass,
            componentClassList,
            childContents,
            firstChildContent,
            firstChildClass,
            firstChildClassList,
            parent,
            parentClass,
            parentClassList,
            children: childContents,
            childrenClassList
        };

        const { canAccept, fixed, hidePadding } = dragAndDropRules.getRuleValue(componentClassList[0], schemaContext);
        canAcceptChildContent = canAccept;
        hideNestedPadding = hidePadding;
        isInFixedContextRules = fixed;
        return true;

        // // 1、子表区域固定层级：container(f-struct-wrapper)--->section(f-section-tabs)--->tab(f-component-tabs)
        // if (cmpClassList.includes('f-component-tabs') && parentClassList.includes('f-section-tabs') && grandParentClassList.includes('f-struct-wrapper')) {
        //     hidePadding = true;
        //     return true;
        // }
    }

    return {
        canAccepts,
        checkCanDeleteComponent,
        checkCanMoveComponent,
        hideNestedPaddingInDesginerView,
        resolveComponentContext,
    } as UseDesignerRules;

}
